#include "Dojo.h"
#include "Sequence.h"
#include <iostream>
using namespace std;

Dojo::Dojo()
{
}

void Dojo::Start()
{
	Sequence AI;
	AI.Generate();

	Sequence player;
	for (int counter = 0; counter < NOOF_MOVES; ++counter)
	{
		int key;
		cout << "Move #" << counter << endl; 
		cout << "Type (1=attack, 2=block): ";
		cin >> key;

		MoveType type;
		switch (key)
		{
		case 1:
			type = ATTACK;
			break;
		case 2:
		default:
			type = BLOCK;
			break;
		};

		cout << "Position (1=high, 2=mid, 3=low): ";
		cin >> key;

		MovePosition position;
		switch (key)
		{
		case 1:
			position = HIGH;
			break;
		case 2:
			position = MID;
			break;
		case 3:
		default:
			position = LOW;
			break;
		};

		cout << endl;

		player.SetMove(counter, type, position);
	}

	FightWinner winner = Fight(&player, &AI);
	switch (winner)
	{
	case Player1:
		cout << "Player 1 wins!" << endl;
		break;
	case Player2:
		cout << "Player 2 wins!" << endl;
		break;
	case Draw:
		cout << "Draw!" << endl;
		break;
	};
}

FightWinner Dojo::Fight(Sequence * i_player1, Sequence * i_player2) const
{
	unsigned int player1wins = 0;
	unsigned int player2wins = 0;

	for (int counter = 0; counter < NOOF_MOVES; ++counter)
	{
		unsigned char player1move = i_player1->GetMove(counter);
		unsigned char player2move = i_player2->GetMove(counter);

		if ((player1move&ATTACK)&&(player2move&ATTACK))
		{
			++player1wins;
			++player2wins;
		}
		else if ((player1move&ATTACK)&&(player2move&BLOCK))
		{
			if ((player1move&LOW)&&(!(player2move&LOW)) ||
				(player1move&MID)&&(!(player2move&MID)) ||
				(player1move&HIGH)&&(!(player2move&HIGH)))
			{
				++player1wins;
			}
		}
		else if ((player2move&ATTACK)&&(player1move&BLOCK))
		{
			if ((player2move&LOW)&&(!(player1move&LOW)) ||
				(player2move&MID)&&(!(player1move&MID)) ||
				(player2move&HIGH)&&(!(player1move&HIGH)))
			{
				++player2wins;
			}
		}

		cout << "Player 1 (" << 100-(player2wins*20) << ") & Player 2 (" << 100-(player1wins*20) << ")" << endl;
	}

	if (player1wins>player2wins)
		return Player1;
	else if (player2wins>player1wins)
		return Player2;
	else
		return Draw;
}

